package com.firmiana.jt809.protocol.receive;

import com.firmiana.jt809.common.MainResponseEnum;
import com.firmiana.jt809.connection.ConnectionManager;
import com.firmiana.jt809.entity.BizConnection;
import com.firmiana.jt809.entity.JT809Message;
import com.firmiana.jt809.entity.MainLinkLoginEntity;
import com.firmiana.jt809.netty.sub.NettyTCPSubLink;
import com.firmiana.jt809.parser.LoginEntityParser;
import com.firmiana.jt809.protocol.IReceiveProtocol;
import com.firmiana.jt809.protocol.send.MainSendRespProtocol;
import com.firmiana.jt809.service.SubLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 主链路登录 业务类型
 *
 * @author hucl
 * @date 2020/8/25 16:25
 */
@Component
@Slf4j
public class MainConnectProtocol implements IReceiveProtocol {

    @Resource
    private SubLinkService subLinkService;

    @Override
    public String handle(JT809Message message) {
        String messageBody = message.getMessageBody();
        String platformId = String.valueOf(message.getMsgGNSSCenterID());

//        MainLinkLoginEntity mainLinkLoginEntity = LoginEntityParser.parse(messageBody);
        MainLinkLoginEntity mainLinkLoginEntity = LoginEntityParser.mock();
        mainLinkLoginEntity.setPlatformId(platformId);
        log.info("下级平台接入信息:{}", mainLinkLoginEntity.toString());

        // 平台登录验证
        MainResponseEnum checkResult = loginCheck(mainLinkLoginEntity);

        // 连接应答消息
        MainSendRespProtocol respProtocol = new MainSendRespProtocol(checkResult.getCode(), Integer.parseInt(platformId));
        JT809Message respMsg = respProtocol.wrap();
        respMsg.setMsgGNSSCenterID(message.getMsgGNSSCenterID());
        respMsg.setMsgDesc("主链路连接请求结果：" + checkResult.getDesc() + "，接入码:" + platformId);

        if (MainResponseEnum.SUCCESS == checkResult) {
            synchronized (this) {
                BizConnection bizConnection = ConnectionManager.get(platformId);
                if (bizConnection == null) {
                    BizConnection con = new BizConnection(platformId);
                    con.setPlatformPort(mainLinkLoginEntity.getSubLinkServerPort());
                    con.setPlatformServerIp(mainLinkLoginEntity.getSubLinkServerIp());

                    // 建立从链路连接
                    NettyTCPSubLink subLink = subLinkService.createSubLink(mainLinkLoginEntity.getSubLinkServerIp(), mainLinkLoginEntity.getSubLinkServerPort());
                    con.setSubLink(subLink);
                    // 保存从链路连接成功的channel信息
                    if (subLink.isRunning()) {
                        // 保存从链路连接信息
                        ConnectionManager.put(platformId, con);
                    }

                }
                // 发送从链路连接请求
                subLinkService.subConnectRequest(platformId);
            }
        }

        return respMsg.toHexString();
    }

    private MainResponseEnum loginCheck(MainLinkLoginEntity entity) {
        //TODO do something to check login info
        return MainResponseEnum.SUCCESS;
    }



}
